package com.kangyi.admin.controller;

import com.kangyi.admin.service.HealthDataService;
import com.kangyi.dto.AddHealthDataDTO;
import com.kangyi.dto.EditHealthDataDTO;
import com.kangyi.dto.HealthDataPageQueryDTO;
import com.kangyi.result.HealthDataPageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.GuestVO;
import com.kangyi.vo.HealthDataDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/health")
@Slf4j
@Api(tags = "养老院-健康数据记录相关接口")
public class HealthDataController {

    @Autowired
    private HealthDataService healthDataService;


    @GetMapping("/page")
    @ApiOperation("健康数据记录分页查询")
    public Result<HealthDataPageResult> pageQuery(HealthDataPageQueryDTO healthDataPageQueryDTO){
        log.info("健康数据记录分页查询:{}",healthDataPageQueryDTO);
        try {
            HealthDataPageResult healthDataPageResult = healthDataService.pageQuery(healthDataPageQueryDTO);
            return Result.success(healthDataPageResult,"查询成功!");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除健康数据记录")
    @CacheEvict(cacheNames = "healthData_", allEntries = true)
    public Result deleteHealthData(@RequestParam List<Integer> ids){
        log.info("删除健康数据记录：{}",ids);
        try {
            healthDataService.deleteHealthData(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{nursingHomeId}")
    @ApiOperation("查询当前养老院下的入住人")
//    @Cacheable(cacheNames = "healthData_", key = "'adminGet_'+#nursingHomeId")
    public Result<List<GuestVO>> getByGuestId(@PathVariable Integer nursingHomeId){
        log.info("查询当前养老院下的入住人:{}",nursingHomeId);
        try {
            List<GuestVO> guestVOList = healthDataService.getByGuestId(nursingHomeId);
            return Result.success(guestVOList,"查询成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增健康数据记录")
    @CacheEvict(cacheNames = "healthData_", allEntries = true)
    public Result addHealthData(AddHealthDataDTO addHealthDataDTO){
        log.info("新增健康数据记录:{}",addHealthDataDTO);
        try {
            healthDataService.addHealthData(addHealthDataDTO);
            return Result.success("新增成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{fourHighId}")
    @ApiOperation("查询健康数据详情")
//    @Cacheable(cacheNames = "healthData_", key = "'adminDetail_'+#fourHighId")
    public Result<HealthDataDetailVO> detailFourHigh(@PathVariable Integer fourHighId){
        log.info("查询健康数据详情：{}",fourHighId);
        try {
            HealthDataDetailVO healthDataDetailVO = healthDataService.detailFourHigh(fourHighId);
            return Result.success(healthDataDetailVO,"查询成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PutMapping("/edit")
    @ApiOperation("修改健康数据记录")
    @CacheEvict(cacheNames = "healthData_", allEntries = true)
    public Result editHealthData(@RequestBody EditHealthDataDTO editHealthDataDTO){
        log.info("修改健康数据记录:{}",editHealthDataDTO);
        try {
            healthDataService.editHealthData(editHealthDataDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
